<?php

class HasManyAssociation extends Association
{

  /**
   *
   * @var boolean
   */
  protected $saveAfter = true;

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/Association#loadData($client)
   */
  protected function loadData(IAggregate $client)
  {
    $dao = DAO::getInstance();

    $table = $this->getDefinition()->getTarget()->getTable();
    $fk = $this->getDefinition()->getForeignKeyField()->getName();
    $id = $client->getId();

    $result = $dao->query()->from($table)->where("$fk = ?")->execute($id);

    $objs = DomainMapper::inflateCollection($this->getDefinition()->getTarget(), $result->fetchAll());

    $meta = & $this->data($client);
    $meta['value'] = $objs;
  }

  /*public function findAll($name, array &$clients)
  {
    $dao = DAO::getInstance();

    $table = $this->getDefinition()->getTarget()->getTable();
    $fk = $this->getDefinition()->getForeignKeyField()->getName();
    $pk = $this->getDefinition()->getPrimaryKeyField()->getName();
    $fields = RepositoryDAO::collectFields($this->getDefinition()->getTarget());

    if (isset($clients[$pk])) {
      $id = $clients[$pk];

      $query = $dao->query()->from($table)->select($fields)->where("$fk = ?");
      $result = $query->execute($id);

      $data = $result->fetchAll();

      $clients[$name] = $data;
    }
    else {
      $ids = array();
      foreach ($clients as $client) {
        $ids[] = $client[$pk];
      }

      if (! empty($ids)) {
        $ids = sy_array_unique($ids);
        $ids = implode(', ', $ids);

        $query = $dao->query()->from($table)->select($fields)->where("$fk IN ($ids)");
        $result = $query->execute();

        $data = $result->fetchAll();

        $_data = array();
        foreach ($data as $row) {
          if (! isset($_data[$row[$fk]])) {
            $_data[$row[$fk]] = array();
          }

          $_data[$row[$fk]][] = $row;
        }
        $data = $_data;
      }

      foreach ($clients as & $client) {
        if (isset($data[$client[$pk]])) {
          $client[$name] = $data[$client[$pk]];
        }
        elseif ($this->getDefinition()->isRequired()) {
          $client[$name] = array();
        }
      }
    }
  }*/

}

?>